<?php
namespace app\shop\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
//use app\shop\model\Artist as BannerModel;
use app\shop\model\Banner as BannerModel;
use think\Hook;

/**
 * 轮播控制器
 */
class Banner extends Admin
{
    public function index()
    {
    	//记录call_back
    	cookie('__forward__', $_SERVER['REQUEST_URI']);

        $data_list = BannerModel::order('sort,id desc')->paginate();

        $category = [1=>'PC首页',2=>'移动端',3=>'移动端轮播'];

        return ZBuilder::make('table')
            ->setPageTitle('轮播管理')
//            ->setSearch(['id' => 'ID', 'title' => '头衔', 'name' => '姓名'])
            ->addColumns([
                ['id', 'ID'],
                ['photo', '头像', 'picture'],
                ['title', '标题'],
                ['group', '分类','select',$category],
                ['url', '链接'],
                ['create_time', '创建时间', 'datetime'],
                ['status', '状态', 'switch'],
                ['right_button', '操作', 'btn']
            ])
            ->addTopButtons('add,enable,disable,delete') // 批量添加顶部按钮
            ->addRightButtons('edit,delete') // 批量添加右侧按钮
            ->setRowList($data_list) // 设置表格数据
            ->fetch(); // 渲染页面
    }

    public function add()
    {
        // 保存数据
        if ($this->request->isPost()) {
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'Banner');
            // 验证失败 输出错误信息
            if(true !== $result) $this->error($result);

            if ($user = BannerModel::create($data)) {
                Hook::listen('banner_user_add', $user);
                // 记录行为
                action_log('banner_user_add', 'artist_user_add', $user['id'], UID);
                $this->success('新增成功', url('index'));
            } else {
                $this->error('新增失败');
            }
        }
        $category = [1=>'PC首页',2=>'移动端',3=>'移动端轮播'];
        return ZBuilder::make('form')
            ->setPageTitle('新增轮播')
            ->addFormItems([
                ['text', 'title', '标题', '必填'],
                ['image', 'photo', '头像'],
                ['text', 'url', '链接地址'],
                ['select', 'group', '分类', '', $category],
                ['text', 'sort', '排序', '', 100],
                ['radio', 'status', '状态', '', ['禁用', '启用'], 1]
            ])
            ->fetch();
    }

    public function edit($id = null)
    {
        if ($id === null) $this->error('缺少参数');

        // 保存数据
        if ($this->request->isPost()) {
            $data = $this->request->post();

            if (BannerModel::update($data)) {
                $user = BannerModel::get($data['id']);
                Hook::listen('banner_user_edit', $user);
                $this->success('编辑成功', cookie('__forward__'));
            } else {
                $this->error('编辑失败');
            }
        }

        // 获取数据
        $info = BannerModel::where('id', $id)->find();

        $category = [1=>'PC首页',2=>'移动端',3=>'移动端轮播'];

        // 使用ZBuilder快速创建表单
        return ZBuilder::make('form')
            ->setPageTitle('编辑') // 设置页面标题
            ->addFormItems([ // 批量添加表单项
            	['hidden', 'id'],

                ['text', 'title', '标题', '必填'],
                ['image', 'photo', '头像'],
                ['text', 'url', '链接地址'],
                ['select', 'group', '分类', '', $category],
                ['text', 'sort', '排序', '', 100],
                ['radio', 'status', '状态', '', ['禁用', '启用'], 1]
            ])
            ->setFormData($info) // 设置表单数据
            ->fetch();
    }

    public function delete($ids = [])
    {
        Hook::listen('artist_user_delete', $ids);
        return $this->setStatus('delete');
    }


    /**
     * 设置用户状态：删除、禁用、启用
     */
    public function setStatus($type = '', $record = [])
    {
        $ids        = $this->request->isPost() ? input('post.ids/a') : input('param.ids');
        $uid_delete = is_array($ids) ? '' : $ids;
        $ids        = array_map('get_nickname', (array)$ids);
        return parent::setStatus($type, ['user_'.$type, 'shop_artist', $uid_delete, UID, implode('、', $ids)]);
    }
}

